package com.game.setting;

import com.game.cache.caffeine.CaffeineCache;
import com.game.cache.layer.LayeringCache;
import com.game.cache.redis.RedisCache;
import com.game.cache.redis.client.RedisProperties;
import com.game.listener.IPublisher;
import com.game.listener.redis.RedisPushMessageListener;
import com.game.listener.redis.RedisPushPublisher;
import com.game.support.CacheMode;
import lombok.Getter;
import lombok.Setter;

/**
 * @author : zGame
 * @version V1.0
 * @Project: game-multi-cache
 * @Package com.game.setting
 * @Description: TODO
 * @date Date : 2022年02月15日 17:48
 */
@Getter
@Setter
public class LayeringCacheSetting implements ICacheSetting<LayeringCache> {
    
    /**
     * 緩存名稱
     */
    private final String cacheName;
    
    /**
     * 内部缓存名
     */
    private final String cacheInternalKey;
    
    
    private final CaffeineCacheSetting localCacheSetting;
    
    private final RedisCacheSetting remoteCacheSetting;
    
    /**
     * 是否使用一级缓存
     */
    CacheMode cacheMode;
    
    private final IPublisher publisher;
    
    /**
     * cache下
     */
    
    public LayeringCacheSetting(CaffeineCacheSetting localCacheSetting, RedisCacheSetting remoteCacheSetting,
            CacheMode cacheMode, String cacheName, String cacheInternalKey, IPublisher publisher) {
        this.localCacheSetting = localCacheSetting;
        this.remoteCacheSetting = remoteCacheSetting;
        this.cacheMode = cacheMode;
        this.cacheName = cacheName;
        this.cacheInternalKey = cacheInternalKey;
        this.publisher = publisher;
    }
    
    @Override
    public LayeringCache createCache() {
        // 创建一级缓存
        CaffeineCache caffeineCache = localCacheSetting.createCache();
        // 创建二级缓存
        RedisCache redisCache = remoteCacheSetting.createCache();
        
        return new LayeringCache(caffeineCache, redisCache, cacheMode, cacheName, publisher);
    }
}
